// 由三个参数组成: 牌型类别,牌型数字,第几张牌
// 1.牌型类别:1=万,2=筒,3=条,4=字牌,5=花牌2.牌型数字:1-9 字牌顺序为:东南西北中发白3.第几张牌
// 最终给出的牌型数字为:牌型类别*100+牌型数字*10+第几张牌
// 例如:
// 第一张一筒=2*100+1*10+1=211第二张四条=3*100+4*10+2=342

import { Vec3, } from 'cc';

import { Zjmj3D } from "./StaticDefine";

enum CardType {
    ERROR = -1,// 错误牌型
    WAN = 1,// 万
    TONG = 2,// 筒  
    TIAO = 3,// 条
}

enum CardValue {
    CARD_1 = 1,
    CARD_2 = 2,
    CARD_3 = 3,
    CARD_4 = 4,
    CARD_5 = 5,
    CARD_6 = 6,
    CARD_7 = 7,
    CARD_8 = 8,
    CARD_9 = 9,
}

enum CardLeft {
    No_Action = 0,
    One_Action = 1,
    Two_Action = 2,
    Three_Action = 3,
    Four_Action = 4,
}

export default class CardLogic {
    static exiled = [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1];
    static offsetX = [0, Zjmj3D.Card.x*12, Zjmj3D.Card.x*12, 0, Zjmj3D.Card.x*9, Zjmj3D.Card.x*9, 0, Zjmj3D.Card.x*6, Zjmj3D.Card.x*6, 0, Zjmj3D.Card.x*3, Zjmj3D.Card.x*3, 0, 0, 0];

    static get CardValue() {
        return CardValue;
    }

    static get CardType() {
        return CardType;
    }

    static ExistExileCard(cardIDs: number[]): boolean {
        const cardCount = cardIDs.length;
        if(this.exiled[cardCount] == 1)
        {
            return true;
        }
        return false;
    }

    static exileCardIndex(cardIDs: number[]): number {
        if(this.ExistExileCard(cardIDs))
        {
            return cardIDs.length - 1;
        }
        else
        {
            return cardIDs.length;
        }
    }

    static getExileCardPosition(cardIDs: number[]): Vec3 {
        return this.getCardPosition(cardIDs.length-1, cardIDs);
    }

    static getCardPosition(cardIndex: number, cardIDs: number[], away: boolean = false): Vec3 {

        const x = away ? Zjmj3D.Card.ax : Zjmj3D.Card.x;

        let pos = new Vec3 (cardIndex * x, 0, 0);

        const cardCount = cardIDs.length;
        const offset = this.offsetX[cardCount];
        pos.x = pos.x + offset;
        // console.log("pos.offset...",cardCount,offset);
        if(this.ExistExileCard(cardIDs) && cardIndex == cardCount - 1)
        {
            pos.x = pos.x + Zjmj3D.Pos.ExileSpace;
        }

        return pos;
    }

    static getCardAngle(): Vec3 {
        return new Vec3(0, 0, 0);
    }

    static getInsertIndex(deleteIndex: number, cardIDs: number[]): number {
        const exileID = cardIDs[cardIDs.length - 1];
        console.log("exileID...",exileID);
        for(let i = 0; i < cardIDs.length; i++)
        {
            const curID = cardIDs[i];
            console.log("curID...",i, curID);

            if(cardIDs[i] == deleteIndex)
            {
                continue;
            }
            if(curID >= exileID)
            {
                return i > 0 ? i - 1: 0;
            }
        }
        return -1;
    }


    static sortCards(cardIDs: number[]) {

        function sortID(carID: number) {
            let cardType = Math.floor(carID / 100);
            let cardValue = Math.floor(carID / 10) % 10;
            let cardIndex = carID % 10;
            const isKing = cardValue == 1 || cardValue == 2;
            //maping
            if(cardType == 2) cardType = 3;
            else if(cardType == 3) cardType = 2;

            return isKing ? cardType * 10 + cardValue : cardType * 100 + cardValue * 10 + cardIndex;
        }

        cardIDs.sort((a, b) => {
            const sortIDa = sortID(a);
            const sortIDb = sortID(b);
            return sortIDa - sortIDb; // 确保 ID 存在
        });
    }

}
